<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">

          <div class="toggle-btn" @click="toggleCollapse" style="text-align:center">|||</div>
        <el-menu
          background-color="#393f4c"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          :default-active="activeTab"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模版区域 -->
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path "
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveActiveTab('/'+subItem.path )"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconList: {
        125: 'iconfont icon-users',
        101: 'iconfont icon-tijikongjian',
        103: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单栏是否折叠
      isCollapse: false,
      // 点击的tab二级菜单高亮
      activeTab: ''
    }
  },
  created () {
    this.getMenuList()
    this.activeTab = window.sessionStorage.getItem('activeTab')
  },
  methods: {
    logout () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取左侧菜单栏数据
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res.data)
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 将点击tab栏的index值保存在session中
    saveActiveTab (index) {
      window.sessionStorage.setItem('activeTab', index)
      this.activeTab = index
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #3f4648;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  > div {
    color: #fff;
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
    background-color: #393f4c;
  .el-menu {
    border-right: 0;
  }
  li{
    text-align: left;
  }
// el-submenu__icon-arrow el-icon-arrow-down
}
.el-main {
  background-color: #ecf0f2;
}
.iconfont{
  margin-right: 10px;
}
.toggle-btn{
  background-color: #535b6d;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  cursor: pointer;
  letter-spacing: .2em;
}
</style>
